package com.benmei.weike.web;

import com.benmei.weike.common.Constants;
import com.benmei.weike.common.Lang;
import com.benmei.weike.common.Page;
import com.benmei.weike.common.filter.RequestPage;
import com.benmei.weike.dto.McScheduleDetailDto;
import com.benmei.weike.exception.ClientException;
import com.benmei.weike.service.McCourseService;
import com.benmei.weike.service.common.MemcachedService;
import com.benmei.weike.util.JsonUtil;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * Create by chenhong on 2018/10/15 15:18
 * Description : 一对多课程   我的
 */
@Controller
public class McCourseController {
    public static final Logger logger = LoggerFactory.getLogger(McCourseController.class);
    @Autowired
    private McCourseService mcCourseService;
    @Autowired
    private MemcachedService memcachedService;

    /**
     * 我的-课程--课列表
     * @param reqPage
     * @param token
     * @return
     */
    @RequestMapping(value = "/studet/my/token/mcCourse/mcSchedule", method = {RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo getScheduleDetai(@RequestBody RequestPage reqPage,
                                    @RequestHeader(value = "token", required = true) String token) {

        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(reqPage));
            //课程Id出错
            Integer courId = reqPage.getMcCourseId();
            if (courId == 0 || null == courId) {
                throw new ClientException("课程id出错或者为空" + reqPage.getMcCourseId());
            }
            //获取学生Id
            Integer memb_id = ((TdNtMember) memcachedService.get(token)).getMemb_id();
            reqPage.setObj(memb_id);

            //查询课程中课列表
            List<McScheduleDetailDto> scheduleList = mcCourseService.getMcSchedulelist(reqPage);

            //数据文件
            Page page = new Page(reqPage.getCurrentPage(), reqPage.getPageSize(), reqPage.getTotalRecord());
            page.setData(scheduleList);
            RetInfo retInfo = RetInfo.getSuccessInfo();
            //在相应文件中设置数据，课信息列表
            retInfo.setObj(page);

            //打印出参信息
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;

        } catch (ClientException e) {   //客户端异常
            RetInfo retInfo = RetInfo.getClientErrorInfo(e.getMessage());
            logger.error(e.getMessage(), e);
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            RetInfo retInfo = RetInfo.getServerErrorInfo(Lang.EN);
            logger.error(e.getMessage(), e);
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * 提交评价
     * @param reqPage
     * @param token
     * @return
     */
    @RequestMapping(value="/studet/my/token/mcSchedule/comment", method = {RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo comment(@RequestBody Map<String,Object> reqPage,
                             @RequestHeader(value="token",required = true) String token){
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(reqPage));

            //课 Id出错
            Object scheduleId= reqPage.get("mc_course_schedule_id");
            if (null==scheduleId) {
                throw new ClientException("课件id出错或者为空,mc_course_schedule_id:" +scheduleId);
            }
            Object schedule_com=reqPage.get("schedule_com");
            Object tea_com=reqPage.get("tea_com");
            //校验评价星级
            if(null!=schedule_com && null!=tea_com){
                int schedule_com_int=Integer.parseInt(schedule_com.toString());
                int tea_com_int=Integer.parseInt(tea_com.toString());
                if(schedule_com_int<1 || schedule_com_int>5 || tea_com_int<1 || tea_com_int >5){
                    throw new ClientException("评价星级超出范围（1-5）,mc_course_schedule_id:" +scheduleId);
                }
            }else{
                throw new ClientException("评价星级出错或者为空,mc_course_schedule_id:" +scheduleId);
            }
            //校验评价内容不能为空
            Object content=reqPage.get("content");// 对课程的评价内存
            Object tea_content=reqPage.get("tea_content");// 对老师的评价
            if(null ==content || null==tea_content || content.toString().length()==0 || tea_content.toString().length()==0){
                throw new ClientException("评价内容不能为空,mc_course_schedule_id:" +scheduleId);
            }

            //从缓存中取出用户信息
            Integer memb_id=((TdNtMember)memcachedService.get(token)).getMemb_id();
            reqPage.put("memb_id",memb_id);
            //业务处理，添加评论
            mcCourseService.addComment(reqPage);

            RetInfo retInfo=RetInfo.getSuccessInfo();
            return retInfo;
        }catch (ClientException e) {   //客户端异常
            RetInfo retInfo = RetInfo.getClientErrorInfo(e.getMessage());
            logger.error(e.getMessage(), e);
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            RetInfo retInfo = RetInfo.getServerErrorInfo(Lang.EN);
            logger.error(e.getMessage(), e);
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}
